﻿using System;
using System.ComponentModel;
using System.Globalization;

namespace TeamDeployer.TeamFoundation.Build.Workflow.CustomTypes
{
   internal class DatabaseSpecConverter:ExpandableObjectConverter 
    {
        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
        {
            return ((destinationType == typeof(string)) ||
                    base.CanConvertTo(context, destinationType));
        }

        public override object ConvertTo(ITypeDescriptorContext context,
                                         CultureInfo culture,
                                         object value,
                                         Type destinationType)
        {
            DatabaseSpec spec = value as DatabaseSpec;
            if ((destinationType != typeof(string)) || (spec == null))
            {
                return base.ConvertTo(context, culture, value, destinationType);
            }


            if (spec.HasConfiguration && spec.Deploy)
            {
                return "Deploys to [ " + spec.TargetServer + " ].[ " + spec.TargetDatabase + " ]";
            }
            else
            {

                if (spec.Deploy && !spec.HasConfiguration)
                {
                    return "Deployment is enabled and Database is not configured";
                }



                if (!spec.Deploy && spec.HasConfiguration)
                {
                    return "Deployment is disabled for [ "+spec.TargetServer +" ].[ "+spec.TargetDatabase +" ]";
                }


                return string.Empty;
            }
        }
    }
}
